package com.nolon.flange.dao

import androidx.lifecycle.LiveData
import androidx.room.*
import com.nolon.flange.model.Order

@Dao
interface OrderDao {
    @Insert
    suspend fun insert(order: Order): Long

    @Insert
    suspend fun insertAll(orders: List<Order>)

    @Delete
    suspend fun delete(order: Order)

    @Query("DELETE FROM order_table WHERE project_id =:projectId")
    suspend fun deleteOrdersByProjectId(projectId: Int)

    @Update(onConflict = OnConflictStrategy.REPLACE)
    suspend fun update(order: Order)

    // 返回LiveData不需要suspend关键字
    @Query("SELECT * FROM order_table WHERE project_id =:projectId")
    fun getOrdersByProjectId(projectId: Int): LiveData<List<Order>>

    //    suspend fun deleteAll(orderItem: List<Project>)
    @Query("SELECT * FROM order_table ORDER BY id DESC")
    fun getAll(): LiveData<List<Order>>

    @Query("SELECT count(*) FROM order_table WHERE project_id=:projectId")
    suspend fun countOrderByProjectId(projectId: Int): Int


}
//just return the LiveData without using Coroutines. LiveData works out of the box, there's no reason to use Coroutines when returning LiveData.